package com.aoxin.service.impl.course.systemCourse;

import com.aoxin.mapper.course.systemCourse.SchoolCourseToPageDao;
import com.aoxin.model.course.SchoolCourseToPage;
import com.aoxin.service.inter.course.systemCourse.SchoolCourseToPageService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.LinkedList;
import java.util.Map;

@Service("SchoolCourseToPageService")
public class SchoolCourseToPageServiceImpl implements SchoolCourseToPageService {
    @Autowired
    private SchoolCourseToPageDao SchoolCourseToPageDao;

    /**
     * 新增学校课程包对应课程
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public Integer insertSchoolCourseToPage(Integer schoolCourseIds[], Integer schoolCoursePageId) throws Exception {
        Integer number = 0;
        Integer result = 0;
        String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
        Assert.notEmpty(schoolCourseIds, ErrorUtil.CHECK_ADD_TO_PAGE);
        Assert.notNull(schoolCoursePageId, ErrorUtil.CHECK_ADD_TO_PAGE);
        if (schoolId.equalsIgnoreCase("0")) {
            throw new Exception(ErrorUtil.AUTHRITY);
        } else {
            for (Integer schoolCourseId : schoolCourseIds) {
                Integer check = SchoolCourseToPageDao.checkSchoolCourseToPage(schoolId, schoolCoursePageId, schoolCourseId);
                if (check > 0) {
                    number++;
                } else {
                    throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
                }
            }
            if (number == schoolCourseIds.length) {
                for (Integer schoolCourseId : schoolCourseIds) {
                    SchoolCourseToPage schoolCourseToPage = new SchoolCourseToPage();
                    schoolCourseToPage.setSchoolId(schoolId);
                    schoolCourseToPage.setSchoolCoursePageId(schoolCoursePageId);
                    schoolCourseToPage.setSchoolCourseId(schoolCourseId);
                    Integer col = SchoolCourseToPageDao.insertSchoolCourseToPage(schoolCourseToPage);
                    if (col > 0) {
                        result++;
                    } else {
                        throw new Exception(ErrorUtil.RESULT_VALUE);
                    }
                }
            } else {
                throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
            }
        }
        return result;
    }

    /**
     * 查询学校课程包对应课程
     * @param schoolCoursePageId
     * @return
     */
    @Override
    public LinkedList<Map<String, Object>> getSchoolCourseToPage(Integer schoolCoursePageId) throws Exception {
        String schoolId = ThreadLocalUtil.getEmployee().getSchoolId().toString();
        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
        LinkedList<Map<String, Object>> list = SchoolCourseToPageDao.getSchoolCourseToPage(schoolId, schoolCoursePageId);
        if (list != null && !list.isEmpty()) {
            return list;
        } else {
            throw new Exception(ErrorUtil.RESULT_VALUE);
        }
    }

    @Override
    @Transactional
    public Integer updateSchoolCourseToPage(Integer schoolCourseToPageId,Integer schoolCourseId) throws Exception {
        Integer number=0;
        String schoolId = ThreadLocalUtil.getEmployee().getSchoolId().toString();
        Integer check = SchoolCourseToPageDao.checkSchoolCourseToPage(schoolId, schoolCourseToPageId, schoolCourseId);
        if(check==0){
            Integer col = SchoolCourseToPageDao.updateSchoolCourseToPage(schoolCourseToPageId);
            if(col>0){
                number++;
            }else{
                throw new Exception(ErrorUtil.RESULT_VALUE);
            }
        }else{
            throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
        }
        return number;
    }
    /**
     * 删除学校课程包对应课程
     * @param schoolCourseToPageId
     * @return
     */
    @Override
    @Transactional
    public Integer deleteSchoolCourseToPage(Integer schoolCourseToPageId) throws Exception {
        Assert.notNull(schoolCourseToPageId,ErrorUtil.CHECK_CSToPgID);
        Integer col = SchoolCourseToPageDao.deleteSchoolCourseToPage(schoolCourseToPageId);
        if(col>0){
            return col;
        }else{
            throw new Exception(ErrorUtil.CHECK_CSToPgID);
        }
    }

	@Override
	@Transactional
	public void insertSchoolCourseToPageDate(Integer[] schoolCourseIds, Integer schoolCoursePageId) throws Exception{
		  String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
	        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
	        Assert.notEmpty(schoolCourseIds, ErrorUtil.CHECK_ADD_TO_PAGE);
	        Assert.notNull(schoolCoursePageId, ErrorUtil.CHECK_ADD_TO_PAGE);
	        for (Integer schoolCourseId : schoolCourseIds) {
	        Integer check = SchoolCourseToPageDao.checkSchoolCourseToPage(schoolId, schoolCoursePageId, schoolCourseId);
	        if(check>=1){
	            throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
	        }else{
	        	 SchoolCourseToPage schoolCourseToPage = new SchoolCourseToPage();
	             schoolCourseToPage.setSchoolId(schoolId);
	             schoolCourseToPage.setSchoolCoursePageId(schoolCoursePageId);
	             schoolCourseToPage.setSchoolCourseId(schoolCourseId);
	           SchoolCourseToPageDao.insertSchoolCourseToPage(schoolCourseToPage);
	        }
	        
	        }
	}
    
    
   
    
}
